package com.me.mvc.model.validator;

import com.me.mvc.model.entity.Client;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * @author Roman Uholnikov
 */
public class ClientValidator implements Validator {

    @Override
    public boolean supports(final Class<?> aClass) {
        return Client.class.isAssignableFrom(aClass);
    }

    @Override
    public void validate(final Object o, final Errors errors) {
        Client client = (Client) o;
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "id", "required.address","!!!!!!!!Id chould not be empty");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "required.name","!!!!!!!!!name chould not be empty");
        if(client.getName().contains("Roman")){
            errors.rejectValue("name", "required.name","there can not be two romans!");
        }
        if(client.getName().contains("Ivan")){
            errors.reject("required.name","there can not be two romans!");
        }
    }
}
